package com.thinkgem.jeesite.modules.cms.web.front;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.security.shiro.session.SessionDAO;
import com.thinkgem.jeesite.common.utils.SpringContextHolder;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.web.Servlets;
import com.thinkgem.jeesite.modules.front.entity.FrontUser;
import com.thinkgem.jeesite.modules.front.service.FrontUserService;
import com.thinkgem.jeesite.modules.front.utils.FrontUserUtils;
import com.thinkgem.jeesite.modules.sys.service.SystemService;

/**
 * 前端用户Controller
 * @author 朱俊吉
 * @version 2017-11-25
 */
@Controller
@RequestMapping(value = "${frontPath}/user")
public class FrontAccountController extends BaseController{
	
	@Autowired
	private FrontUserService frontUserService;
	@Autowired
	private SessionDAO sessionDAO;
	
	private SystemService systemService;
	
	/**
	 * 验证登录名是否有效
	 * @param oldLoginName
	 * @param loginName
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "checkLoginName")
	public String checkLoginName(String oldAccount, String account) {
		if (account !=null && account.equals(oldAccount)) {
			return "true";
		} else if (account !=null && frontUserService.getUserByAccount(account) == null) {
			return "true";
		}
		return "false";
	}

	@RequestMapping(value = "save")
	public String save(FrontUser frontUser, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, frontUser)){
			addMessage(model, "提交信息有误!");
			return "modules/cms/front/themes/wending/regist";
		}
		frontUser.setPassword(SystemService.entryptPassword(frontUser.getPassword()));
		
		System.out.println(frontUser.getAccount());
		if (!"true".equals(checkLoginName(frontUser.getOldAccount(), frontUser.getAccount()))){
			addMessage(model, "注册用户'" + frontUser.getAccount() + "'失败，用户名已存在");
			return "modules/cms/front/themes/wending/regist";
		}
		frontUserService.save(frontUser);
		addMessage(redirectAttributes, "注册成功");
		return "redirect:"+Global.getFrontPath()+"/front-regist.html/?repage";
	}
	
	@RequestMapping(value = "login", method = RequestMethod.POST)
	public String login(FrontUser frontUser, HttpServletRequest request, HttpServletResponse response, Model model) {
		
		FrontUser byLoginName = FrontUserUtils.getByLoginName(frontUser.getAccount());
		//根据账号查询不要用户
		if(byLoginName == null){
			addMessage(model, "账号密码有误!");
			return "modules/cms/front/themes/wending/frontIndex";
		}
		
		//验证没密码
		String entryptPassword = byLoginName.getPassword();
		String plainPassword = frontUser.getPassword();
		if(!SystemService.validatePassword(plainPassword, entryptPassword)){
			addMessage(model, "账号密码有误!");
			return "modules/cms/front/themes/wending/frontIndex";
		}
		
		//更新用户登陆信息
		getSystemService().updateFrontUserLoginInfo(byLoginName);
		
		//将用户信息存入session
		request.getSession().setAttribute(FrontUserUtils.FRONT_USER_INFO, byLoginName);
		
		//重定向到首页
		return "redirect:"+frontPath+"/index-3.html/?repage";
	}
	/**
	 * 管理登录
	 */
	@RequestMapping(value = "login", method = RequestMethod.GET)
	public String login(HttpServletRequest request, HttpServletResponse response, Model model) {
		
		FrontUser frontUserInfo = (FrontUser)request.getSession().getAttribute("frontUserInfo");
		
		if (logger.isDebugEnabled()){
			logger.debug("login, active session size: {}", sessionDAO.getActiveSessions(false).size());
		}
		
		// 如果已登录，再次访问主页，则退出原账号。暂时未用到
//		if (Global.TRUE.equals(Global.getConfig("notAllowRefreshIndex"))){
//			CookieUtils.setCookie(response, "FRONTLOGINED", "false");
//		}
		
		// 如果已经登录，则跳转到管理首页
		if(frontUserInfo != null){
			return "redirect:" + frontPath;
		}

		return "modules/cms/front/themes/wending/frontIndex";
	}
	
	/**
	 * 获取系统业务对象
	 */
	public SystemService getSystemService() {
		if (systemService == null){
			systemService = SpringContextHolder.getBean(SystemService.class);
		}
		return systemService;
	}

}
